﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VehicleManagement.Models;
using VehicleManagement.Codes;
using VehicleManagement.ControllersServices;
using System.Collections;
namespace VehicleManagement.Controllers
{
    //[CustomerAuthorize(Roles = "负责人")]
    public class CarInsureController : Controller
    {
        const int PAGE_MAX = 8;
        CarInsureServices insureServices = new CarInsureServices();
        //
        // GET: /CarInsure/

        public ViewResult Index()
        {
            Hashtable ht = new Hashtable();
            int page;
            if (!string.IsNullOrWhiteSpace(Request.Params["Page"]))
                page = int.Parse(Request.Params["Page"]);
            else
                page = 1;
            SearchInsuresByRequeast(ref ht);
            
            return View(insureServices.SearchAllInsures(ht, (page - 1) * PAGE_MAX, PAGE_MAX));

        }
        /// <summary>
        /// 按条件搜索
        /// </summary>
        /// <param name="ht"></param>
        [NonAction]
        private void SearchInsuresByRequeast(ref Hashtable ht)
        {
            string carNum = Request.Params["carNum"];
            string insureType = Request.Params["insureType"];
            string apploval = Request.Params["apploval"];

            string numForSearch = "%%";
            string typeForSearch = "%%";
            int applovalForSearch = 3;
            if (apploval == "已审批")
                applovalForSearch = 1;
            else if (apploval == "未审批")
                applovalForSearch = 0;
            if (!string.IsNullOrEmpty(carNum) && carNum != "车牌号")
                numForSearch = string.Format("%{0}%", carNum);
            if (!string.IsNullOrEmpty(insureType) && insureType != "保险类别")
                typeForSearch = string.Format("%{0}%", insureType);

            ht.Add("numForSearch", numForSearch);
            ht.Add("typeForSearch", typeForSearch);
            ht.Add("applovalForSearch", applovalForSearch);
            if (Session["CompanyName"] == null)
            {
                return;
            }
            ht.Add("companyName", Session["CompanyName"].ToString());
        }

        /// <summary>
        /// 判断保单号是否重复
        /// </summary>
        /// <param name="InvoiceNo"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetCARInvoiceNo(string InvoiceNo)
        {
            //IList<CarInsure> lstModel = SqlMapperManager.Instance.Get().QueryForList<CarInsure>("SEARCH_CarInsure", null);
            //List<CarInsure> lstModel2 = lstModel.Where(u => u.InvoiceNo.ToLower() == InvoiceNo.ToLower()).ToList();
            //return Json(lstModel2.Count == 0, JsonRequestBehavior.AllowGet);
            Hashtable ht = new Hashtable();
            ht.Add("invoiceNo", InvoiceNo);
            ht.Add("companyName", Session["CompanyName"].ToString());

            int count = Convert.ToInt32(SqlMapperManager.Instance.Get().QueryForObject("SELECT_COUNT_CARINVOICENO", ht));

            return Json(count == 0, JsonRequestBehavior.AllowGet);
        }

        //
        // GET: /CarInsure/Details/5

        public ViewResult Details(int id)
        {
            return View(insureServices.GetInsuresByCarInsureID(id));
        }

        //
        // GET: /CarInsure/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /CarInsure/Create

        [HttpPost]
        public ActionResult Create(CarInsure carinsure)
        {
            if (ModelState.IsValid)
            {
            if (Session["CompanyName"] != null)
                carinsure.CompanyName = Session["CompanyName"].ToString();
            else
                return View("../Home/Login");

            object obj = insureServices.Create(carinsure);
            return RedirectToAction("Index");
            }

            return View(carinsure);
        }

        //
        // GET: /CarInsure/Edit

        public ActionResult Edit(int id)
        {
            return View(insureServices.GetInsuresByCarInsureID(id));
        }

        //
        // POST: /CarInsure/Edit

        [HttpPost]
        public ActionResult Edit(CarInsure carinsure)
        {
            if (ModelState.IsValid)
            {
            int rows = insureServices.Edit(carinsure);
            return RedirectToAction("Index");
            }
            return View(carinsure);
        }

        //
        // GET: /CarInsure/Delete

        public ActionResult Delete(int id)
        {

            return View(insureServices.GetInsuresByCarInsureID(id));
        }

        //
        // POST: /CarInsure/Delete

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            if (Request.IsAjaxRequest())
            {
                int i = insureServices.Delete(id);
                if (i > 0)
                {
                    return Json(new { Result = "true", Message = "删除成功" });
                }
                else
                {
                    return Json(new { Result = "false", Message = "删除失败" });
                }
            }
            else
            {
                return Json(new { Result = "false", Message = "该方法只能通过AJAX方式调用" });
            }
        }

        [ActionName("GetPagingInfo"), HttpGet]
        public ActionResult GetPagingInfo()
        {
            int currentPage;
            if (string.IsNullOrWhiteSpace(Request.Params["Page"]))
                currentPage = 1;
            else
                currentPage = int.Parse(Request.Params["Page"]);

            Hashtable ht = new Hashtable();
            SearchInsuresByRequeast(ref ht);
            int itemCount = insureServices.CountAllInsure(ht);
            return Json(new
            {
                TotalItems = itemCount,
                ItemsPerPage = PAGE_MAX,
                CurrentPage = currentPage,
                TotalPages = (int)Math.Ceiling((decimal)itemCount / PAGE_MAX)
            }, JsonRequestBehavior.AllowGet);
        }

    }
}
